package com.hostmann.corejava;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;
import java.util.Objects;

/**
 * Employee class has five private fileds
*/
public class Employee extends Person implements Comparable<Employee>{
//    private static int nextId = 1;
    private static int nextId;
    private double salary;
    private Date hireDay;
    private int id;
    
    static {
        Random generator = new Random();
        nextId = generator.nextInt(10000);
        
    }
    
    {
        id = nextId;
        nextId++;
        Random generator = new Random();
        GregorianCalendar calender = new GregorianCalendar(generator.nextInt(10) + 2000, generator.nextInt(11), generator.nextInt(31));
        hireDay = calender.getTime();
    }
    
    public Employee (String aName, double aSalary) {
        super(aName);
        salary = aSalary;
    }
    
    public Employee (double aSalary) {
        this("Employee #" + nextId, aSalary);
    }
    public Employee (String n, double s, int year, int month, int day) {
        super(n);
        salary = s;
        GregorianCalendar calender = new GregorianCalendar(year, month -1, day);
        hireDay = calender.getTime();
        id = 0;
    }
    
    public String getDescription () {
        return String.format("an employee %s with a salary of $%.2f", super.getName(), salary);
    }
    public double getSalary() {
        return salary;
    }
    /**
     * @param no param
     * @return Date
     */
    public Date getHireDay() {
        return (Date)hireDay.clone();
    }
    
    public int getId() {
        return id;
    }
    
    public void setId() {
        id = nextId;
        nextId++;
    }
    
    public static int getNextId() {
        return nextId;
    }
    /**
     * raise employee salary
     * @param double byPercent, percent by which the salary is raised to 
     * @return no return
     */
    public void raiseSalary(double byPercent) {
        double raise = salary * byPercent /100;
        salary += raise;
    }
    
    public boolean equals (Object otherObject) {
        if (this == otherObject) return true;
        
        if (otherObject == null) return false;
        
        if (this.getClass() != otherObject.getClass()) return false;
        
        Employee other = (Employee) otherObject;
        return Objects.equals(getName(), other.getName()) && Objects.equals(hireDay, other.getSalary()) && salary == other.salary;
    }
    
    public int hashCode () {
        return Objects.hash(getName(), salary, hireDay);
    }
    
    public String toString () {
        return getClass().getName() + "[name=" + getName() + ",salary=" + salary + ",hireday=" + hireDay + "]";
    }
    
    public int compareTo (Employee other) {
        return Double.compare(this.salary, other.salary);
    }
}

